﻿@layout DeployLayout

@page "/Deploy/UploadPackage"

@using Tewr.Blazor.FileReader
@using System.IO
@using Deploy.HttpManage
@using Materal.APP.HttpClient
@using Materal.Common
@using Materal.Model
@using WebAPP.Common

@inject MessageManage _messageManage
@inject ExceptionManage _exceptionManage
@inject IFileReaderService _fileReaderService
@inject IApplicationInfoManage _applicationInfoManage

<main class="layout_body">
    <MPageHeader Title="上传应用程序包">
        <div class="m_button_group">
            <MButton Disabled="Loading" Class="m_button_icon Micon Micon_clear" Title="清理上传文件" OnClick="@OnBtnClearUpdateFilesClickAsync"></MButton>
            <MButton Disabled="Loading" Class="m_button_icon Micon Micon_clear_all" Title="清理不活跃程序" ButtonType="@MButtonTypeEnum.Danger" OnClick="@OnBtnClearInactiveApplicationClickAsync"></MButton>
        </div>
    </MPageHeader>
    <MUploadFile Disabled="Loading" Accept=".rar" @ref="@UploadFileElement" OnChange="@OnSelectedFileChange"></MUploadFile>
</main>

@code {
    #region 属性
    /// <summary>
    /// 加载中
    /// </summary>
    public bool Loading { get; set; }
    /// <summary>
    /// 上传文件元素
    /// </summary>
    public MUploadFile UploadFileElement;
    #endregion
    #region 事件
    /// <summary>
    /// 选择文件完毕
    /// </summary>
    /// <returns></returns>
    public async Task OnSelectedFileChange()
    {
        try
        {
            IFileReference file = await UploadFileElement.GetFileReferenceAsync();
            if (file == null) return;
            IFileInfo fileInfo = await file.ReadFileInfoAsync();
            await using MemoryStream memoryStream = await file.CreateMemoryStreamAsync();
            var formFile = new MateralFormFile(memoryStream, "file", fileInfo.Name);
            await UploadNewFileAsync(formFile);
        }
        catch (WebAPPException exception)
        {
            await _messageManage.WarningAsync(exception.Message);
        }
        catch (Exception exception)
        {
            await _exceptionManage.HandlerExceptionAsync(exception);
        }
    }
    /// <summary>
    /// 单击清理上传文件按钮
    /// </summary>
    /// <param name="eventArgs"></param>
    /// <returns></returns>
    public async Task OnBtnClearUpdateFilesClickAsync(MouseEventArgs eventArgs)
    {
        await ClearUpdateFilesAsync();
    }
    /// <summary>
    /// 单击清理不活跃程序按钮
    /// </summary>
    /// <param name="eventArgs"></param>
    /// <returns></returns>
    public async Task OnBtnClearInactiveApplicationClickAsync(MouseEventArgs eventArgs)
    {
        await ClearInactiveApplicationAsync();
    }
    #endregion
    #region 私有方法
    /// <summary>
    /// 上传新文件
    /// </summary>
    /// <param name="formFile"></param>
    /// <returns></returns>
    public async Task UploadNewFileAsync(MateralFormFile formFile)
    {
        Loading = true;
        StateHasChanged();
        try
        {
            ResultModel resultModel = await _applicationInfoManage.UploadNewFileAsync(formFile);
            if (resultModel.ResultType == ResultTypeEnum.Success)
            {
                await _messageManage.SuccessAsync(resultModel.Message);
            }
            else
            {
                await _messageManage.WarningAsync(resultModel.Message);
            }
        }
        catch (Exception exception)
        {
            await _exceptionManage.HandlerExceptionAsync(exception);
        }
        finally
        {
            Loading = false;
            StateHasChanged();
        }
    }
    /// <summary>
    /// 清理上传文件
    /// </summary>
    /// <returns></returns>
    public async Task ClearUpdateFilesAsync()
    {
        Loading = true;
        try
        {
            ResultModel resultModel = await _applicationInfoManage.ClearUpdateFilesAsync();
            if (resultModel.ResultType == ResultTypeEnum.Success)
            {
                await _messageManage.SuccessAsync(resultModel.Message);
            }
            else
            {
                await _messageManage.WarningAsync(resultModel.Message);
            }
        }
        catch (Exception exception)
        {
            await _exceptionManage.HandlerExceptionAsync(exception);
        }
        finally
        {
            Loading = false;
        }
    }
    /// <summary>
    /// 清理不活跃程序
    /// </summary>
    /// <returns></returns>
    public async Task ClearInactiveApplicationAsync()
    {
        Loading = true;
        try
        {
            ResultModel resultModel = await _applicationInfoManage.ClearInactiveApplicationAsync();
            if (resultModel.ResultType == ResultTypeEnum.Success)
            {
                await _messageManage.SuccessAsync(resultModel.Message);
            }
            else
            {
                await _messageManage.WarningAsync(resultModel.Message);
            }
        }
        catch (Exception exception)
        {
            await _exceptionManage.HandlerExceptionAsync(exception);
        }
        finally
        {
            Loading = false;
        }
    }
    #endregion
}
